﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserManage.aspx.cs" Inherits="ProjectStudy.UserManage.UserManage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

    <style type="text/css">
table.altrowstable {
	font-family: verdana,arial,sans-serif;
	font-size:14px;
	color:#333333;
	border-width: 1px;
	border-color: #a9c6c9;
	border-collapse: collapse;
}
table.altrowstable th {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #a9c6c9;
}
table.altrowstable td {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #a9c6c9;
}
.oddrowcolor{
	background-color:#d4e3e5;
}
.evenrowcolor{
	background-color:#c3dde0;
}
</style>

    <script type="text/javascript">
        $(function () {

            $.ajax({
                type: "post",
                url: "/Ajax/Menu_Manage.ashx",
                data: { "action": "GetGroup" },
                dataType: "json",
                success: function (data) {
                    $.each(data, function (index, item) {
                        $("#sel1").append('<option id="' + 'op' + (index + 1) + '">' + item.GroupName + '</option>');
                        $("#sel2").append('<option id="' + 'op' + (index + 1) + '">' + item.GroupName + '</option>');
                    });
                }
            });

            $("#btnSearch").click(function () {
                FnsearchData();
            });

            $("#btnActive").click(function () {
                FnUserActive();
            });

            $("#btnLock").click(function () {
                FnUserLock("Y");
            });

            $("#btnUnLock").click(function () {
                FnUserLock("N");
            });

            $("#btnDel").click(function () {
                FnUserDel();
            });

            $("#btnSet").click(function () {
                FnUserGroupChange();
            });
        });

        function FnsearchData() {
            $("#tb1").html("");
            var requirement = $("#sel1").find("option:selected").text();
            $.ajax({
                type: "post",
                url: "/Ajax/Ajax_UserManage.ashx",
                data: { "action": "GetUserList", "group": requirement },
                dataType: "json",
                success: function (data) {
                    //var json = eval("(" + data + ")");
                    if (data != null && data != '') {
                        $("#tb1").append('<tr id="' + 'tr' + 0 + '"></tr>');
                        $("#tr0").append('<td>' + '<input type="checkbox" id="cb0"/>' + '</td>');
                        $("#tr0").append('<td>' + '用户名' + '</td>');
                        $("#tr0").append('<td>' + '用户组' + '</td>');
                        $("#tr0").append('<td>' + '邮箱' + '</td>');
                        $("#tr0").append('<td>' + '问题' + '</td>');
                        $("#tr0").append('<td>' + '答案' + '</td>');
                        $("#tr0").append('<td>' + '激活状态' + '</td>');
                        $("#tr0").append('<td>' + '锁定状态' + '</td>');
                        $("#tr0").append('<td>' + '注册时间' + '</td>');
                        $("#cb0").click(function () {
                            $("input[name='chkItem']").attr("checked", $("#cb0").attr("checked"));
                        });

                        $.each(data, function (index, item) {
                            var isactive = item.isActive == "N" ? "未激活" : "已激活";
                            var islock = item.isLock == "N" ? "未锁定" : "已锁定";
                            var registerT = item.registerTime == null ? '' : item.registerTime.replace('T', ' ');
                            $("#tb1").append('<tr id="' + 'tr' + (index + 1) + '"></tr>');
                            $("#" + "tr" + (index + 1)).append('<td>' + '<input type="checkbox" id="' + 'cb' + (index + 1) + '" name="chkItem" />' + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td>' + item.username + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td>' + item.group + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td>' + item.email + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td>' + item.question + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td>' + item.answer + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td id="tdactive'+index+'">' + isactive + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td id="tdlock'+index+'">' + islock + '</td>');
                            $("#" + "tr" + (index + 1)).append('<td>' + registerT + '</td>');
                            if (isactive == "已激活") {
                                $("#tdactive"+index).css("color", "Green");
                            }
                            else {
                                $("#tdactive"+index).css("color", "Red");
                            }
                            if (islock == "未锁定") {
                                $("#tdlock" + index).css("color", "Green");
                            }
                            else {
                                $("#tdlock" + index).css("color", "Red");
                            }
                        });
                        
                        
                    }
                    else {
                        alert("没有数据");
                    }
                }
            });
        }

        function FnUserActive() {
            var chks = $("input[name='chkItem']");
            var arrUsers = [];
            var arrTr = [];
            for (var i = 0; i < chks.length; i++) {
                if (chks[i].checked) {
                    arrTr.push($(chks[i]).parent().parent().attr('id'));
                    arrUsers.push($(chks[i]).parent().next().html());
                }
            }
            if (arrUsers.length <= 0) {
                alert("请最少选择一个账号");
            }
            else {
                if (confirm("确认激活?") == true) {
                    $.ajax({
                        type: "Post",
                        url: "../Ajax/Ajax_UserManage.ashx",
                        data: { "action": "UserActive", "users": JSON.stringify(arrUsers), "isActive": "Y" },
                        dataType: "json",
                        success: function (data) {
                            if (data["status"] == "success") {
                                for (var i = 0; i < arrTr.length; i++) {
                                    $("#" + arrTr[i]).children().first().next().next().next().next().next().next().html("已激活");
                                    $("#" + arrTr[i]).children().first().next().next().next().next().next().next().css("color","Green");
                                }
                            }
                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                }
            }
        }

        function FnUserLock(isLock) {
            var chks = $("input[name='chkItem']");
            var arrUsers = [];
            var arrTr = [];
            for (var i = 0; i < chks.length; i++) {
                if (chks[i].checked) {
                    arrTr.push($(chks[i]).parent().parent().attr('id'));
                    arrUsers.push($(chks[i]).parent().next().html());
                }
            }
            if (arrUsers.length <= 0) {
                alert("请最少选择一个账号");
            }
            else {
                var msg = "";
                var res = "";
                if (isLock == "Y") {
                    msg = "确认锁定？";
                    res = "已锁定";
                }
                else {
                    msg = "确认解锁？";
                    res = "未锁定";
                }
                  if (confirm(msg) == true) {
                      $.ajax({
                          type: "Post",
                          url: "../Ajax/Ajax_UserManage.ashx",
                          data: { "action": "UserLock", "users": JSON.stringify(arrUsers), "isLock": isLock },
                          dataType: "json",
                          success: function (data) {
                              if (data["status"] == "success") {
                                  for (var i = 0; i < arrTr.length; i++) {
                                      $("#" + arrTr[i]).children().first().next().next().next().next().next().next().next().html(res);
                                      if (res == "已锁定") {
                                          $("#" + arrTr[i]).children().first().next().next().next().next().next().next().next().css("color", "Red");
                                      }
                                      else {
                                          $("#" + arrTr[i]).children().first().next().next().next().next().next().next().next().css("color", "Green");
                                      }
                                      //alert($("#" + arrTr[i]).children().first().next().next().next().next().next().next().html());
                                  }
                              }
                          },
                          error: function (err) {
                              alert(err);
                          }
                      });
                  }
                
            }
          }

          function FnUserDel() {
              var chks = $("input[name='chkItem']");
              var arrUsers = [];
              var arrTr = [];
              for (var i = 0; i < chks.length; i++) {
                  if (chks[i].checked) {
                      arrTr.push($(chks[i]).parent().parent().attr('id'));
                      arrUsers.push($(chks[i]).parent().next().html());
                  }
              }
              if (arrUsers.length <= 0) {
                  alert("请最少选择一个账号");
              }
              else {
                  if (confirm("确认删除?") == true) {
                      $.ajax({
                          type: "Post",
                          url: "../Ajax/Ajax_UserManage.ashx",
                          data: { "action": "UserDel", "users": JSON.stringify(arrUsers) },
                          dataType: "json",
                          success: function (data) {
                              if (data["status"] == "success") {
                                  for (var i = 0; i < arrTr.length; i++) {
                                      $("#" + arrTr[i]).remove();
                                  }
                              }
                          },
                          error: function (err) {
                              alert(err);
                          }
                      });
                  }
              }
          }

          function FnUserGroupChange() {
              var chks = $("input[name='chkItem']");
              var arrUsers = [];
              var arrTr = [];
              for (var i = 0; i < chks.length; i++) {
                  if (chks[i].checked) {
                      arrTr.push($(chks[i]).parent().parent().attr('id'));
                      arrUsers.push($(chks[i]).parent().next().html());
                  }
              }
              if (arrUsers.length <= 0) {
                  alert("请最少选择一个账号");
              }
              else {
                  if (confirm("确认更新用户组?") == true) {
                      var group = $("#sel2").find("option:selected").text();
                      $.ajax({
                          type: "Post",
                          url: "../Ajax/Ajax_UserManage.ashx",
                          data: { "action": "UserGroupChange", "users": JSON.stringify(arrUsers), "group": group },
                          dataType: "json",
                          success: function (data) {
                              if (data["status"] == "success") {
                                  for (var i = 0; i < arrTr.length; i++) {
                                      $("#" + arrTr[i]).children().first().next().next().html(group);
                                  }
                              }
                          },
                          error: function (err) {
                              alert(err);
                          }
                      });
                  }
              }
          }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <label style=" height:30px; line-height:30px; font-size:16px;">用户组</label>
    <select id="sel1" style=" height:30px; line-height:30px; font-size:16px;">
      <option id="op0" selected="selected">all</option>
    </select>
    <input  type="button" id="btnSearch" value="搜索" style=" height:30px; line-height:30px; font-size:16px; text-align:center;"/>
    <br />
    <br />
    <input  type="button" value="激活" id="btnActive" style=" height:30px; line-height:30px; font-size:16px;"/>
    <input  type="button" value="锁定" id="btnLock" style=" height:30px; line-height:30px; font-size:16px;"/>
    <input  type="button" value="解锁" id="btnUnLock" style=" height:30px; line-height:30px; font-size:16px;"/>
    <input  type="button" value="删除" id="btnDel" style=" height:30px; line-height:30px; font-size:16px;"/>
    <br />
    <br />
    <select id="sel2" style=" height:30px; line-height:30px; font-size:16px;"></select>
    <input  type="button" id="btnSet" value="设置" style=" height:30px; line-height:30px; font-size:16px;"/>
    <br />
    <br />
    <table id="tb1" border="1" cellpadding="0" cellspacing="0" class="altrowstable">
     
    </table>
    </div>
    </form>
</body>
</html>
